﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Input;

namespace MedVis
{
    public partial class MainWindow : Window
    {
        private void resetCropButton_Click(object sender, RoutedEventArgs e)
        {
            tb_cropBot.Value = 0;
            tb_cropLeft.Value = 0;
            tb_cropRight.Value = 0;
            tb_cropTop.Value = 0;
            unsaved = true;
        }

        private void refreshCropPos()
        {
            System.Windows.Vector v = imageDisplay.PointToScreen(new System.Windows.Point(imageDisplay.ActualWidth, imageDisplay.ActualHeight)) - imageDisplay.PointToScreen(new System.Windows.Point(0, 0));
            Thickness margin = new Thickness(0, 0, 0, 0);
            margin.Left = (window.ActualWidth - v.X) / 2 - 233;
            wp_cropLeft.Margin = margin;
            if (margin.Left > 0)
                wp_cropLeftFill.Width = margin.Left;
            else
                wp_cropLeftFill.Width = 0;

            margin.Right = (window.ActualWidth - v.X) / 2 + 217;
            wp_cropRight.Margin = margin;
            if ((margin.Right - 450) > 0)
                wp_cropRightFill.Width = margin.Right - 450;
            else
                wp_cropRightFill.Width = 0;

            margin.Top = (window.ActualHeight - v.Y) / 2 - 25;
            wp_cropTop.Margin = margin;
            if ((margin.Top - 21) > 0)
                wp_cropTopFill.Height = margin.Top - 21;
            else
                wp_cropTopFill.Height = 0;

            margin.Bottom = (window.ActualHeight - v.Y) / 2 - 15;
            wp_cropBot.Margin = margin;
            if ((margin.Bottom - 31) > 0)
                wp_cropBotFill.Height = margin.Bottom - 31;
            else
                wp_cropBotFill.Height = 0;

            Refresh(wp_cropBotFill);
            Refresh(wp_cropTopFill);
            Refresh(wp_cropLeftFill);
            Refresh(wp_cropRightFill);
        }

        private void resetCropPos()
        {
            wp_cropLeftFill.Width = 0;
            wp_cropRightFill.Width = 0;
            wp_cropBotFill.Height = 0;
            wp_cropTopFill.Height = 0;

            Refresh(wp_cropBotFill);
            Refresh(wp_cropTopFill);
            Refresh(wp_cropLeftFill);
            Refresh(wp_cropRightFill);
        }

        private void vb_current_MouseDown(object sender, MouseButtonEventArgs e)
        {
            cursorPosOld = Mouse.GetPosition(System.Windows.Application.Current.MainWindow);
            if (e.RightButton == MouseButtonState.Pressed)
            {
                markRect.Visibility = Visibility.Visible;
            }
        }

        private void vb_current_MouseUp(object sender, MouseButtonEventArgs e)
        {
            if (markRect.Visibility == Visibility.Visible)
            {
                cursorPosNew = Mouse.GetPosition(System.Windows.Application.Current.MainWindow);
                markRect.Width = 0;
                markRect.Height = 0;
                markRect.Margin = new Thickness(0, 0, 0, 0);
                markRect.Visibility = Visibility.Hidden;

                Thickness margin;
                if (cursorPosOld.X < cursorPosNew.X && cursorPosOld.Y < cursorPosNew.Y)
                {
                    margin = wp_cropLeft.Margin;
                    if (cursorPosOld.X - margin.Left < 0)
                        wp_cropLeft.Width = 0;
                    else
                        wp_cropLeft.Width = cursorPosOld.X - margin.Left;
                    margin = wp_cropTop.Margin;
                    if (cursorPosOld.Y - margin.Top < 0)
                        wp_cropTop.Height = 0;
                    else
                        wp_cropTop.Height = cursorPosOld.Y - margin.Top;
                    margin = wp_cropRight.Margin;
                    if (grid.ActualWidth - cursorPosNew.X - margin.Right < 0)
                        wp_cropRight.Width = 0;
                    else
                        wp_cropRight.Width = grid.ActualWidth - cursorPosNew.X - margin.Right;
                    margin = wp_cropBot.Margin;
                    if (grid.ActualHeight - cursorPosNew.Y - margin.Bottom < 0)
                        wp_cropBot.Height = 0;
                    else
                        wp_cropBot.Height = grid.ActualHeight - cursorPosNew.Y - margin.Bottom;
                }
                if (cursorPosOld.X > cursorPosNew.X && cursorPosOld.Y < cursorPosNew.Y)
                {
                    margin = wp_cropLeft.Margin;
                    if (cursorPosNew.X - margin.Left < 0)
                        wp_cropLeft.Width = 0;
                    else
                        wp_cropLeft.Width = cursorPosNew.X - margin.Left;
                    margin = wp_cropTop.Margin;
                    if (cursorPosOld.Y - margin.Top < 0)
                        wp_cropTop.Height = 0;
                    else
                        wp_cropTop.Height = cursorPosOld.Y - margin.Top;
                    margin = wp_cropRight.Margin;
                    if (grid.ActualWidth - cursorPosOld.X - margin.Right < 0)
                        wp_cropRight.Width = 0;
                    else
                        wp_cropRight.Width = grid.ActualWidth - cursorPosOld.X - margin.Right;
                    margin = wp_cropBot.Margin;
                    if (grid.ActualHeight - cursorPosNew.Y - margin.Bottom < 0)
                        wp_cropBot.Height = 0;
                    else
                        wp_cropBot.Height = grid.ActualHeight - cursorPosNew.Y - margin.Bottom;
                }
                if (cursorPosOld.X < cursorPosNew.X && cursorPosOld.Y > cursorPosNew.Y)
                {
                    margin = wp_cropLeft.Margin;
                    if (cursorPosOld.X - margin.Left < 0)
                        wp_cropLeft.Width = 0;
                    else
                        wp_cropLeft.Width = cursorPosOld.X - margin.Left;
                    margin = wp_cropTop.Margin;
                    if (cursorPosNew.Y - margin.Top < 0)
                        wp_cropTop.Height = 0;
                    else
                        wp_cropTop.Height = cursorPosNew.Y - margin.Top;
                    margin = wp_cropRight.Margin;
                    if (grid.ActualWidth - cursorPosNew.X - margin.Right < 0)
                        wp_cropRight.Width = 0;
                    else
                        wp_cropRight.Width = grid.ActualWidth - cursorPosNew.X - margin.Right;
                    margin = wp_cropBot.Margin;
                    if (grid.ActualHeight - cursorPosOld.Y - margin.Bottom < 0)
                        wp_cropBot.Height = 0;
                    else
                        wp_cropBot.Height = grid.ActualHeight - cursorPosOld.Y - margin.Bottom;
                }
                if (cursorPosOld.X > cursorPosNew.X && cursorPosOld.Y > cursorPosNew.Y)
                {
                    margin = wp_cropLeft.Margin;
                    if (cursorPosNew.X - margin.Left < 0)
                        wp_cropLeft.Width = 0;
                    else
                        wp_cropLeft.Width = cursorPosNew.X - margin.Left;
                    margin = wp_cropTop.Margin;
                    if (cursorPosNew.Y - margin.Top < 0)
                        wp_cropTop.Height = 0;
                    else
                        wp_cropTop.Height = cursorPosNew.Y - margin.Top;
                    margin = wp_cropRight.Margin;
                    if (grid.ActualWidth - cursorPosOld.X - margin.Right < 0)
                        wp_cropRight.Width = 0;
                    else
                        wp_cropRight.Width = grid.ActualWidth - cursorPosOld.X - margin.Right;
                    margin = wp_cropBot.Margin;
                    if (grid.ActualHeight - cursorPosOld.Y - margin.Bottom < 0)
                        wp_cropBot.Height = 0;
                    else
                        wp_cropBot.Height = grid.ActualHeight - cursorPosOld.Y - margin.Bottom;
                }
                unsaved = true;
            }
        }
    }
}
